package com.example.blank.Dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.example.blank.utils.DBConnectionUtils;
import com.example.blank.utils.DaoUtils;

public class StudentDao {
	
	public static final long serialVersionUID = 1L;
	public static String url = "jdbc:mysql://localhost:3306/studentchoosecourse";
//	public static Statement statement = null;
//	public static Connection connection = null;
//	public static ResultSet resultSet = null;
	
	
	
	public static ArrayList getAllCourse()
	{
		Statement statement = null;
		Connection connection = null;
		ResultSet resultSet = null;
		try{
			connection = DBConnectionUtils.getConnection(connection);
			statement = connection.createStatement();
			resultSet = statement.executeQuery("select course_id, cour_name, teacher_id from course");
			System.out.println(resultSet.toString());
			final ArrayList<HashMap<String, Object>> list = new ArrayList<>();
			resultSet.last();
			if(resultSet.getRow()<=0)
			{
				System.out.println("+++++++++++++++++++0");
				return null;
			}
			resultSet.first();
			do{
				HashMap<String ,Object> map = new HashMap<>();
				map.put("course_id", resultSet.getString(1));
				map.put("course_name", resultSet.getString(2));
				map.put("teacher_id", resultSet.getString(3));
				list.add(map);
				
			}while(resultSet.next());
			
			System.out.println(list.toString());
			return list;
			
			
		}catch(Exception e)
		{
			System.out.println("----------------"+e.getMessage().toString());
			return null;
		}
	}
	
	
	public static boolean  testSQL(HttpServletRequest request, HttpServletResponse response )
	{
		try{
			Class.forName("com.mysql.jdbc.Driver");
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		
		return true;
	}
	
	public static boolean judgeStudentLogin(String id, String pwd)
	{
		Statement statement = null;
		Connection connection = null;
		ResultSet resultSet = null;
		try{
			
			connection = DriverManager.getConnection(url,DaoUtils.sql_name,DaoUtils.sql_pwd);
			statement = connection.createStatement();
			resultSet = statement.executeQuery("select student_id, student_pwd from student");
			while(resultSet.next())
			{
				if(id.equals(resultSet.getString(1))&&pwd.equals(resultSet.getString(2)))
					return true;
			}
			
			
			return false;
			
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}finally{
			
		}
	}
	
	public static HashMap getUserDetail(String id, String pwd)
	{
		Statement statement = null;
		Connection connection = null;
		ResultSet resultSet = null;
		try{
			
			connection = DriverManager.getConnection(url,DaoUtils.sql_name,DaoUtils.sql_pwd);
			statement = connection.createStatement();
			resultSet = statement.executeQuery("select student_id, student_name, student_pwd, student_message, person_id from student");
			while(resultSet.next())
			{
				if(id.equals(resultSet.getString(1))&&pwd.equals(resultSet.getString(3)))
				{
					HashMap map = new HashMap();
					map.put("student_id", resultSet.getString(1));
					map.put("student_name", resultSet.getString(2));
					map.put("student_pwd", resultSet.getString(3));
					map.put("student_message", resultSet.getString(4));
					map.put("person_id", resultSet.getString(5));
					return map;
				}
			}
			
//			HashMap map = new HashMap();
//			map.put("student_id", "123");
//			map.put("student_name", "123");
//			map.put("student_pwd", "123");
//			map.put("student_message", "123");
//			map.put("persion_id", "123");
			
			return null;
			
		}catch(Exception e)
		{
			e.printStackTrace();
			System.out.println("+++++++++++++++++++++++++"+e.getMessage());
			return null;
		}
	}

}
